import axios from 'axios'

//在开发环境中的测试 development
if(process.env.NODE_ENV == 'development') {
	axios.defaults.baseURL = 'http://106.15.195.184:10010'
}
//在生产环境中的测d试 production
// if(process.env.NODE_ENV == 'production') {
// 	axios.defaults.baseURL = ''
// }

//响应超时的时间
axios.defaults.timeout = 5000;

//接口请求拦截
axios.interceptors.request.use(
	config => {
		const token = localStorage.getItem('token')
		if(token) config.headers.token = token;
		
		return config
	}
)
// 响应拦截器
axios.interceptors.response.use((res) => {
	// console.log('响应拦截器：');
	if (res.headers.token) {
		var token = res.headers.token
//      console.log(token)
		window.localStorage.setItem('token', token)
	}
	return res
})

export function get(url,params){
	return new Promise((resolve,reject) => {
		axios.get(url,{
			params : params
		}).then(res => {
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}
export function post(url,params){
	return new Promise((resolve,reject) => {
		axios.post(url,params)
		.then(res => {
			resolve(res.data)
		})
		.catch(err => {
			reject(err.data)
		})
	})
}

export function put(url,params) {
	return new Promise((resolve,reject) => {
		axios.put(url,params)
		.then(res => {
			resolve(res.data)
		})
		.catch(err => {
			reject(err.data)
		})
	})
}